package demo;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.Arrays;

/**
 * @author lingkang
 * @create by 2024/5/13 10:46
 */
public class Demo11 {
    public static void main(String[] args) throws Exception {
        // 初始化密钥库
        KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType());
        caKs.load(
                Files.newInputStream(new File("C:\\Users\\Administrator\\Desktop\\temp\\jks\\lingkang.keystore").toPath()),
                "123456".toCharArray());
        // 初始化TrustManagerFactory
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(caKs);
        // 初始化SSLContext
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, tmf.getTrustManagers(), new SecureRandom());

        // 打印这个SSLContext实例使用的协议
        System.out.println("缺省安全套接字使用的协议: " + sslContext.getProtocol());
        // 获取SSLContext实例相关的SSLEngine
        SSLEngine e = sslContext.createSSLEngine();
        System.out
                .println("支持的协议: " + Arrays.asList(e.getSupportedProtocols()));
        System.out.println("启用的协议: " + Arrays.asList(e.getEnabledProtocols()));
        System.out.println("支持的加密套件: "
                + Arrays.asList(e.getSupportedCipherSuites()));
        System.out.println("启用的加密套件: "
                + Arrays.asList(e.getEnabledCipherSuites()));
    }
}
